#!/bin/bash

oldConfArrary=("" "" "")
newConfArrary=("" "" "")

function GetOldConfInfoFromLine() {
    if [[ -z ${oldConfLine} || $1 == "\n" || $1 == "#" ]]; then
        return 0
    fi

    if [ ${1:0:1} = "[" ]; then
        oldConfArrary[0]=$1
        oldConfArrary[0]=${oldConfArrary[0]: 1: ${#oldConfArrary[0]}-2}
    else
        oldConfArrary[1]=$1
        oldConfArrary[2]=$3
    fi
}

function GetNewConfInfoFromLine() {
    if [[ -z ${newConfLine} || $1 == "\n" || $1 == "#" ]]; then
        return 0
    fi

    if [ ${1:0:1} = "[" ]; then
        newConfArrary[0]=$1
        newConfArrary[0]=${newConfArrary[0]: 1: ${#newConfArrary[0]}-2}
    else
        newConfArrary[1]=$1
        newConfArrary[2]=$3
    fi
}

function MigrateConf2NewConfFile() {
    while read oldConfLine
    do
        GetOldConfInfoFromLine ${oldConfLine}
        if [[ ${oldConfArrary[0]} && ${oldConfArrary[1]} && ${oldConfArrary[2]} ]]; then
                while read newConfLine
                do
                    GetNewConfInfoFromLine $newConfLine
                    if [[ ${oldConfArrary[0]} = ${newConfArrary[0]} && ${oldConfArrary[1]} = ${newConfArrary[1]} ]]; then
                        if [ ${oldConfArrary[0]} = "VERIFICATION" ]; then
                            return 0
                        fi
                        if [[ ${oldConfArrary[2]} = ${newConfArrary[2]} ]]; then
                            break
                        fi
                        su - ${username} -s /bin/bash -c "export LD_LIBRARY_PATH=${KMSAGENT_LIB_PATH}; ${kmsagent} -c ${newConf} -k ${newKmsagentConfKsf} -s ${oldConfArrary[0]} -n ${oldConfArrary[1]} -v ${oldConfArrary[2]}" >/dev/null 2>&1
                        if [ "$?" != 0 ]; then
                            exit -1
                        fi
                        oldConfArrary=(${oldConfArrary[0]} "" "")
                    fi
                done < ${newConf}
        fi
    done < ${oldConf}
}

function CheckConfIntegrity() {
    while read oldConfLine
    do
        GetOldConfInfoFromLine ${oldConfLine}
        if [[ ${oldConfArrary[0]} && ${oldConfArrary[1]} && ${oldConfArrary[2]} ]]; then
            su - ${username} -s /bin/bash -c "export LD_LIBRARY_PATH=${KMSAGENT_LIB_PATH}; ${kmsagent} -c ${oldConf} -k ${oldKmsagentConfKsf} -s ${oldConfArrary[0]} -n ${oldConfArrary[1]} -v ${oldConfArrary[2]}" >/dev/null 2>&1
            return $?
        fi
    done < ${oldConf}
}

function CheckParamInvalid() {
    for var in ${kmsagent} ${oldConf} ${oldKmsagentConfKsf} ${newConf} ${newKmsagentConfKsf}
    do
        if [ ! -f ${var} ]; then
            echo "[WARNING] ${var##*/} is not exist."
            return 1
        fi
    done

    if [[ -z ${username} || -z ${usergroup} ]]; then
        echo "[WARNING] username/usergroup is Invalid"
        return 1
    fi

    if [ -L ${oldConf} ]; then
        echo "[WARNING] ${oldConf##*/} cannot be a soft link."
        return 1
    fi

    if [ -L ${oldKmsagentConfKsf} ]; then
        echo "[WARNING] ${oldKmsagentConfKsf##*/} cannot be a soft link."
        return 1
    fi
    chown -h ${username}:${usergroup} ${oldKmsagentConfKsf}
    su ${username} -s /bin/bash -c "chmod 600 ${oldKmsagentConfKsf}" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "[WARNING] Unable to configure old kmsagent conf ksf."
    fi

    if [ -L ${newConf} ]; then
        echo "[WARNING] ${newConf##*/} cannot be a soft link."
        return 1
    fi
    chown -h ${username}:${usergroup} ${newConf}
    su ${username} -s /bin/bash -c "chmod 600 ${newConf}" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "[WARNING] Unable to configure new kmsagent conf."
    fi

    if [ -L ${newKmsagentConfKsf} ]; then
        echo "[WARNING] ${newKmsagentConfKsf##*/} cannot be a soft link."
        return 1
    fi
    chown -fh ${username}:${usergroup} ${newKmsagentConfKsf}
    su ${username} -s /bin/bash -c "chmod 600 ${newKmsagentConfKsf}" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "[WARNING] Unable to configure new kmsagent conf ksf."
    fi
}

function usage() {
    echo "Usage:"
    echo "     kmsagent_backup_conf.sh [kmsagent] [old/kmsagent.conf] [old/kmsAgent.conf.ksf] [new/kmsAgent.conf] [new/kmsAgent.conf.ksf]" \
         "[username] [usergroup] [kmsagent_lib_path]"
    exit -1
}

if [ $# -ne 8 ]; then
    echo "Incorrect number of parameters."
    usage
fi

kmsagent=$1
oldConf=$2
oldKmsagentConfKsf=$3
newConf=$4
newKmsagentConfKsf=$5
username=$6
usergroup=$7
KMSAGENT_LIB_PATH=$8

CheckParamInvalid
if [ "$?" != 0 ]; then
    exit -1
fi

CheckConfIntegrity
if [ "$?" != 0 ]; then
    exit -1
fi
MigrateConf2NewConfFile
